﻿using System.Collections.Generic;
using System.Linq;

namespace YouTubeDownloadExtension
{
    public static class VideoSourceExtensions
    {
        public static VideoSource SpecificOrBest(this IEnumerable<VideoSource> sources, VideoQuality quality, VideoFormat format)
        {
            return sources.Specific(quality, format) ?? sources.Best();
        }

        public static VideoSource Specific(this IEnumerable<VideoSource> sources, VideoQuality quality, VideoFormat format)
        {
            return sources.FirstOrDefault(source => source.Quality == quality && source.Format == format);
        }

        public static VideoSource Best(this IEnumerable<VideoSource> sources)
        {
            return (from VideoSource source in sources
                    orderby source.Quality descending, source.Format descending
                    select source).First();
        }
    }
}